// pages/biz/unboundCard/unboundCard.js
import Serv from './unboundCardSer.js';
var event = require('../../../../utils/event.js');
import util from '../../../../utils/util2'
import config from '../../../../utils/config2'
var regeneratorRuntime = require("../../../../lib/js/runtime-module.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 银行卡信息
    detailInfo:{},
    name:'',
    user_id:'',
    password:{},
    random_key: "",//密码随机因子,
    inputDisable:false,
    userInfo:{},
  },
  
  // 表单输入
  bindinput: function (e){
    console.log('bindinput',e.currentTarget.dataset,e.detail)
    let label=e.currentTarget.dataset.label
    let value=e.detail.value
    this.setData({
      [label]: value,
    })
  },
  // 密码控件
  pwdFocus: function(e) {
    let label=e.currentTarget.dataset.label
    util.navigateStateChange(this, 'inputDisable');
    wx.navigateTo({
      // from：当前地址， eventName: 监听定义的事件名
      url: '../../pwdCtrl/pwdCtrl?from=/unboundCard/unboundCard&eventName=onPwdChange'+`&label=${label}`
    })
  },
  //密码控件回调
  pwdCtrlCallback: function () {
  
  },
  // 解绑请求数据
  async unboundDataSubmit(params) {
    let dataList = await Serv.unboundDataSubmit(params)
    let { data, msg, success } = dataList
    console.log(dataList)
    if(success){
      wx.showModal({
        title: '提示',
        content: '操作成功',
        showCancel: false,
        complete: (res)=> {
          wx.redirectTo({
            url: '../myBankCard/myBankCard'
          })
        }
      })
    }else{
      wx.showToast({
        title: msg || '',
        icon: 'none',
      })
      return false
    }
  },
  
  // 点击提交数据解绑银行卡
  formSubmit:function(e){
    let submitData = e.detail.value;//name  cardStyle  bank  bankCard  password
    let {user_id,name,password,random_key}=this.data
    let {linked_acctno  , }=this.data.detailInfo
    console.log('submitData',submitData)
    let params ={//user_id  linked_acctno  password  random_key
      user_id : user_id,
      linked_acctno : linked_acctno,
      password: password.pwdMw || '',
      random_key: password.randomKey || '',
    }
    this.unboundDataSubmit(params)
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    // ban'k'card'Detail页面跳转的数据传到options
    let user_id = wx.getStorageSync(config.userIdKey)
    let name = (wx.getStorageSync('walletInfo')||{}).name
    let {item={}}=options
    console.log('onload',JSON.parse(item))
    this.setData({
      user_id : user_id,
      name : name,
      detailInfo:JSON.parse(item)
    })
    //监听密码控件输入
    var that = this
    event.on('onPwdChange', that, function (changedData) {
      let {pwdMw = '', label = '', randomKey = '',pwdHash='',pwdlen=''} = changedData
      console.log('DataChanged trigger...', changedData, {
        [label]: pwdMw
      })
      that.setData({
        [label]: {
          pwdMw: pwdMw,
          randomKey: randomKey,
          pwdHash: pwdHash,
          pwdlen: pwdlen,
          pwdMw_new: pwdMw.substr(0,pwdlen),
        },
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this
    //取消监听
    event.remove('onPwdChange', that)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  //
  // }
})
